<?php

namespace App\Http\Models\Role;

use Illuminate\Support\Facades\DB;
use App\Http\Models\Role\Role;
use App\Http\Models\Permissions\Permissions;
use App\Http\Models\RolePermissions\RolePermissions;
use App\Common\Utils\Common;
use App\Http\Models\Log\LogService;

/*
 * RoleService  角色操作服务只处理逻辑
 *
 * index() 角色列表
 * show() 角色查看
 *
 * @author 金飞虎
 * @email 870741306@qq.com
 * @date 2018/4/5
 * */

class RoleService extends Role
{
    public function index($request)
    {
        return Role::select($this->hidden)
            ->where('role_name', 'like', '%' . $request->role_name . '%')
            ->orderBy('created_at', 'desc')
            ->paginate(PAGES);
    }

    public function store($request)
    {
        $data = [
            'role_name' => $request->role_name,
            'role_explain' => $request->role_explain
        ];
        //开启事物
        DB::beginTransaction();
        try {
            $res = Role::create($data);
            if ($res->id) {
                foreach ($request->permission as $permission) {
                    $permissionData = [
                        'roleid' => $res->id,
                        'permissionsid' => $permission,
                    ];
                    RolePermissions::create($permissionData);
                }
                Common::success(INSERT_SUCCES);
                //操作日志
                LogService::log()->create(INSERT_ROLE . '-' . $request->role_name);
                //事物提交
                DB::commit();
            } else {
                Common::error(INSERT_ERROR);
            }
        } catch (Exception $ex) {
            //事物回滚
            DB::rollBack();
            Common::error(INSERT_ERROR);
        }
    }

    public function updateSave($request, $id)
    {
        $data = [
            'role_name' => $request->role_name,
            'role_explain' => $request->role_explain
        ];
        DB::beginTransaction();
        try {
            $res = Role::where('id', decryptUrl($id))->update($data);
            if ($res) {
                RolePermissions::where('roleid', decryptUrl($id))->delete();
                foreach ($request->permission as $permissions) {
                    $permissionsData = [
                        'roleid' => decryptUrl($id),
                        'permissionsid' => $permissions,
                    ];
                    RolePermissions::create($permissionsData);
                }
                Common::success(UPDATE_SUCCES);
                LogService::log()->create(INSERT_ROLE . '-' . $request->role_name); //操作日志
                DB::commit();
            } else {
                Common::error(UPDATE_ERROR);
            }
        } catch (Exception $ex) {
            Common::error(UPDATE_ERROR);
            DB::rollBack();
        }
    }

    public function show($id)
    {
        return Role::with('getPermission')->where('id', decryptUrl($id))->first();
    }

    //获取所有权限
    public function getPermissionAll()
    {
        return Permissions::where('is_menus', 1)->orderBy("created_at", 'desc')->get();
    }

    public function remove($id)
    {
        $res = Role::where('id', decryptUrl($id))->delete();
        if ($res) {
            LogService::log()->create(DELETE_ROLE);
            return $res;
        } else {
            return $res;
        }
    }
}
